package game

import (
	"fmt"
	"net/http"
	"server/conf"
	"server/msg"

	// "server/msg"
	"encoding/json"
	"strconv"

	"github.com/gorilla/mux"
)

type HttpManager struct {
	Rounter *mux.Router
}

func (p *HttpManager) InitRounter() {
	p.Rounter = mux.NewRouter()
	p.InitFriendRouter()
	p.InitPlayerProfileInfo()
	http.ListenAndServe(conf.Server.HttpPort, p.Rounter)
}

func (p *HttpManager) InitFriendRouter() {
	bs := p.Rounter.PathPrefix("/Friend").Subrouter()
	bs.HandleFunc("/Add/{SenderId}/{ReceiveId}", p.AddFriend)
	bs.HandleFunc("/Del/{SenderId}/{ReceiveId}", p.DeleteFriend)
}

func (p *HttpManager) InitPlayerProfileInfo() {
	bs := p.Rounter.PathPrefix("/Profile").Subrouter()
	bs.HandleFunc("/GetBrief/{Id}", p.GetPlayerProfileInfo)
}

func (p *HttpManager) GetPlayerProfileInfo(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	Id, ok := vars["Id"]

	if ok {
		Uin, _ := strconv.Atoi(Id)
		data := G_GameLogicPtr.MPlayerProfileManager.HttpReqPlayerBriefProfileData(int32(Uin))
		test := []*msg.ResPlayerBriefProfileData{data, data}
		bytes, _ := json.Marshal(test)

		v := []*msg.ResPlayerBriefProfileData{}
		json.Unmarshal(bytes, &v)

		fmt.Println("bytes Length:" + strconv.Itoa(len(v)))
		fmt.Println("bytes Length:" + strconv.Itoa(len(v)))
		w.Write(bytes)
	}
}

func (p *HttpManager) AddFriend(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	SenderId, ok := vars["SenderId"]
	if ok {
		fmt.Println("AddFreind SenderId:" + SenderId)
	}

	ReceiveId, ok1 := vars["ReceiveId"]
	if ok1 {
		fmt.Println("AddFreind ReceiveId:" + ReceiveId)
	}
}

func (p *HttpManager) DeleteFriend(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, ok := vars["id"]
	if !ok {
		fmt.Println("id is missing in parameters")
	}
	fmt.Println(`id := `, id)

}
